package com.mojo.concurrent.lock;

/**
 * 死锁示例
 *
 * @author <a href="mailto:sjj@jianzhimao.com">mojo</a>
 * copyright (C), 2013-2022, 广州九尾信息科技有限公司
 * @date 2022/12/12 19:02
 */
public class DeadLockDemo {
    static final Object o1 = new Object();
    static final Object o2 = new Object();
    static final Object o3 = new Object();

    public static void main(String[] args) {
        Thread r1 = new Thread(() -> {
            synchronized (o1) {
                System.out.println("T1获取o1锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o2) {
                    System.out.println("T1获取o2锁");
                }
            }

        });
        Thread r2 = new Thread(() -> {
            synchronized (o2) {
                System.out.println("T2获取o2锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o3) {
                    System.out.println("T2获取o3锁");
                }
            }

        });
        Thread r3 = new Thread(() -> {
            synchronized (o3) {
                System.out.println("T3获取o3锁");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                synchronized (o1) {
                    System.out.println("T3获取o1锁");
                }
            }

        });
        r1.start();
        r2.start();
        r3.start();
    }
}
